package com.example.vv.model.home.base.presenter;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.vv.R;
import com.example.vv.fragmentWork.entity.ArticleEntity;
import com.example.vv.fragmentWork.entity.BannerEntity;
import com.example.vv.fragmentWork.entity.TopEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.fragmentWork.mvp.ViewISNullException;
import com.example.vv.fragmentWork.utlis.LogUtils;
import com.example.vv.model.home.base.contract.HomeContract;
import com.example.vv.model.home.base.model.HomeModel;
import com.example.vv.res.net.HttpUtil;
import com.example.vv.res.net.NetUrl;
import com.example.vv.res.net.BaseResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HomePresenter extends MvpPresenter<HomeContract.View, HomeModel> implements HomeContract.Presenter {
    List<String> titles = new ArrayList<>();
    List<String> images = new ArrayList<>();

    @Override
    public void attach(HomeContract.View view) {
        super.attach(view);
        model = new HomeModel();
    }


    @Override
    public void setBannerData() {
        model.REQUEST_BANNER(NetUrl.GET_API_BANNER)
                .map(new Function<BaseResponse, List<BannerEntity>>() {
                    @Override
                    public List<BannerEntity> apply(BaseResponse baseResponse) throws Exception {
//                        LogUtils.Log(baseResponse.getData());
                        return new Gson().fromJson(baseResponse.getData(), new TypeToken<List<BannerEntity>>() {
                        }.getType());
                    }
                })
                .subscribe(new Consumer<List<BannerEntity>>() {
                    @Override
                    public void accept(List<BannerEntity> bannerEntity) throws Exception {
                        view().getBannerData(bannerEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtils.Log(throwable.getMessage() + "ggggg");
                    }
                });
    }

    @Override
    public List<String> setTitleData(List<BannerEntity> bannerEntity) {
        for (BannerEntity entity : bannerEntity) {
            String title = entity.getTitle();
            titles.add(title);
        }

        return titles;
    }

    @Override
    public List<String> setImageData(List<BannerEntity> bannerEntity) {
        for (BannerEntity entity : bannerEntity) {
            String imagePath = entity.getImagePath();
            images.add(imagePath);
        }
        return images;
    }

    //请求置顶数据
    @Override
    public void setTopData() {
        model.REQUEST_TOP(NetUrl.GET_API_TOP)
                .map(new Function<BaseResponse, List<TopEntity>>() {
                    @Override
                    public List<TopEntity> apply(BaseResponse baseResponse) {
                        // LogUtils.Log(baseResponse.getData());
                        List<TopEntity> o = new Gson().fromJson(baseResponse.getData(), new TypeToken<List<TopEntity>>() {
                        }.getType());
                        return o;
                    }
                })
                .subscribe(new Consumer<List<TopEntity>>() {
                    @Override
                    public void accept(List<TopEntity> topEntity) {
                        try {
                            view().getTopData(topEntity);

                        } catch (ViewISNullException e) {
                            e.printStackTrace();

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        LogUtils.Log(throwable.getMessage());
                    }
                });
    }

    //请求首页文章列表数量数据
    @Override
    public void setArticleData(String i) {
        model.REQUEST_ARTICLE("article/list/" + i + "/json")
                .map(new Function<BaseResponse, ArticleEntity>() {
                    @Override
                    public ArticleEntity apply(BaseResponse baseResponse) {
                        ArticleEntity articleEntity = new Gson().fromJson(baseResponse.getData(), ArticleEntity.class);
                        Log.i("TAG", "apply: "+"第"+i+"次"+ articleEntity);
                        return articleEntity;
                    }
                })
                .subscribe(new Consumer<ArticleEntity>() {
                    @Override
                    public void accept(ArticleEntity articleentity) {
                        try {
                            view().getArticleData(articleentity);

                        } catch (ViewISNullException e) {
                            e.printStackTrace();
                            LogUtils.Log(e.getMessage());

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        LogUtils.Log(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getImageData() {
        String requestBingPic = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkhttpRequest(requestBingPic, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("sdfsdfa", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String bingPic = response.body().string();
                try {
                    view().getImageData(bingPic);
                } catch (ViewISNullException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
